package cn.he4966.ssq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
public class Http {
    @Autowired
    private ObjectMapper objectMapper; // 自动装配ObjectMapper对象

    @RequestMapping("/http_get")
    public String http_get(String url) throws JsonProcessingException {
        Map<String, Object> data= new HashMap<>();
        data.put("code",1);
        Document doc = null;
        try {
            doc = Jsoup.connect(url)
                    .followRedirects(true).get();//to follow redirects
            String html = doc.body().html();
            data.put("data",html);
        } catch (IOException e) {
            //            throw new RuntimeException(e);
            data.put("code",0);
            data.put("data",e.getMessage());
        }
        return objectMapper.writeValueAsString(data);
    }


    //JSONObject json = new JSONObject(str);

    @RequestMapping("/ssq_get")
    public String ssq_get(Integer page) throws JsonProcessingException {
        Map<String, Object> data= new HashMap<>();
        data.put("code",1);
        Document doc = null;
        try {
            if(page==null)
                page = 1;
            String url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo="+page+"&pageSize=30&week=&systemType=PC";
            doc = Jsoup.connect(url)
                    .followRedirects(true).get();//to follow redirects
            String html = doc.body().html();

            System.out.println(html);


            data.put("data",html);
        } catch (IOException e) {
            //            throw new RuntimeException(e);
            data.put("code",0);
            data.put("data",e.getMessage());
        }
        return objectMapper.writeValueAsString(data);
    }
}
